package com.github.niefy.modules.wx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.niefy.common.utils.PageUtils;
import com.github.niefy.common.utils.Query;
import com.github.niefy.modules.wx.dao.SubTemplateMsgLogMapper;
import com.github.niefy.modules.wx.entity.SubTemplateMsgLog;
import com.github.niefy.modules.wx.service.SubTemplateMsgLogService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class SubTemplateMsgLogServiceImpl extends ServiceImpl<SubTemplateMsgLogMapper, SubTemplateMsgLog> implements SubTemplateMsgLogService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String appid = (String) params.get("appid");
        String touser = (String) params.get("touser");
        String thirdId = (String) params.get("thirdId");
        String templateId = (String) params.get("templateId");
        String sendResult = (String) params.get("sendResult");
        IPage<SubTemplateMsgLog> page = this.page(
                new Query<SubTemplateMsgLog>().getPage(params),
                new QueryWrapper<SubTemplateMsgLog>()
                        .eq(StringUtils.hasText(appid), "appid", appid)
                        .eq(StringUtils.hasText(touser), "touser", touser)
                        .eq(StringUtils.hasText(thirdId), "third_id", thirdId)
                        .eq(StringUtils.hasText(templateId), "template_id", templateId)
                        .eq(StringUtils.hasText(sendResult), "send_result", sendResult)
        );

        return new PageUtils(page);
    }

    /**
     * 记录log，异步入库
     *
     * @param log
     */
    @Override
    @Async("taskExecutor")
    public void addLog(SubTemplateMsgLog log) {
        this.baseMapper.insert(log);
    }
}
